const autoprefixer = require('autoprefixer');
const pxtorem = require('postcss-pxtorem');
const CompressionPlugin = require('compression-webpack-plugin');

module.exports = {
  publicPath: '/h5Chaint',
  devServer: {
    proxy: {
      '/iot_': {
        // target: 'http://s2.v3.hignton.com:9080',
        target: 'http://59.36.87.106:9080',
        ws: true,
        changeOrigin: true
      }
    }
  },
  productionSourceMap: false,
  chainWebpack: config => {
    // 移除 prefetch 插件
    config.plugins.delete('prefetch');
  },
  configureWebpack: {
    plugins: [
      new CompressionPlugin({
        filename: '[path][base].gz',
        algorithm: 'gzip',
        test: /\.js$|\.html$|\.css$|\.jpg$|\.jpeg$|\.png/, // 匹配文件名
        threshold: 10240, // 对超过10k的数据压缩
        minRatio: 0.8
      })
    ],
    // 开启分离 js
    optimization: {
      runtimeChunk: 'single',
      splitChunks: {
        chunks: 'all',
        maxInitialRequests: Infinity,
        minSize: 20000,
        cacheGroups: {
          vendor: {
            test: /[\\/]node_modules[\\/]/,
            name(module) {
              // get the name. E.g. node_modules/packageName/not/this/part.js
              // or node_modules/packageName
              const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1];
              // npm package names are URL-safe, but some servers don't like @ symbols
              return `npm.${packageName.replace('@', '')}`;
            }
          }
        }
      }
    }
  },
  css: {
    loaderOptions: {
      postcss: {
        plugins: [
          autoprefixer({
            overrideBrowserslist: ['Android 4.0', 'iOS 7', 'Chrome > 31', 'ff > 31', 'ie >= 8']
          }),
          pxtorem({
            rootValue: 37.5 /*37.5对应的是375尺寸的图，如果是750尺寸的图则替换成75，以此类推*/,
            propList: ['*']
          })
        ]
      }
    }
  }
};
